package com.jack.leetcode.每日一题;

public class 验证回文字符串2 {

    public static void main(String[] args) {
        String s = "aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga";
        System.out.println(new StringBuffer(s).reverse().toString());
        System.out.println(validPalindrome(s));
        s = "aba";
        System.out.println(validPalindrome(s));
        s = "abcda";
        System.out.println(validPalindrome(s));
        s = "deeee";
        System.out.println(validPalindrome(s));

    }

    public static boolean validPalindrome(String s) {
        int n = s.length();
        boolean first = true;
        boolean second = true;
        int index = -1;
        String r = new StringBuilder(s).reverse().toString();
        for(int i=0;i<n;i++){
            if(s.charAt(i)!=r.charAt(i)){
                index = i;
                break;
            }
        }
        if(index != -1){
            String k = new StringBuilder(s).replace(index,index+1,"").toString();
            String l = new StringBuilder(s).replace(n-index-1,n-index,"").toString();
            int left1 = 0;
            int right1 = n - left1 - 2;
            while (left1 < right1) {
                if (k.charAt(left1) != k.charAt(right1)) {
                    first = false;
                    break;
                } else {
                    left1++;
                    right1--;
                }
            }

            int left2 = 0;
            int right2 = n - left2 - 2;
            while (left2 < right2) {
                if (l.charAt(left2) != l.charAt(right2)) {
                    second = false;
                    break;
                } else {
                    left2++;
                    right2--;
                }
            }
        }else{
            return true;
        }
        return first|second;
    }
}
